package com.liqi.web.controller.device;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liqi.common.core.domain.R;
import com.liqi.device.domain.DvDrive;
import com.liqi.device.domain.DvDriveSdk;
import com.liqi.device.service.IDvDriveSdkService;
import com.liqi.device.service.IDvDriveService;
import com.liqi.web.controller.common.minio.MinIoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.liqi.common.annotation.Log;
import com.liqi.common.core.controller.BaseController;
import com.liqi.common.core.domain.AjaxResult;
import com.liqi.common.enums.BusinessType;

import com.liqi.common.utils.poi.ExcelUtil;
import com.liqi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 驱动管理Controller
 *
 * @author ruoyi
 * @date 2022-10-22
 */
@Api(tags = "驱动插件sdk注册控制器")
@RestController
@RequestMapping("/device/sdk")
public class DvDriveSdkController extends BaseController
{
    @Autowired
    private IDvDriveSdkService dvDriveSdkService;

    @Autowired(required = false)
    private MinIoService minIoService;


    @Autowired
    private IDvDriveService dvDriveService;


    /**
     * 查询驱动管理列表
     */
    @ApiOperation("查询list")
//    @PreAuthorize("@ss.hasPermi('system:sdk:list')")
    @GetMapping("/list")
    public TableDataInfo list(DvDriveSdk dvDriveSdk)
    {
        startPage();
        List<DvDriveSdk> list = dvDriveSdkService.selectDvDriveSdkList(dvDriveSdk);
        return getDataTable(list);
    }

    /**
     * 导出驱动管理列表
     */
    @ApiOperation("导出")
//    @PreAuthorize("@ss.hasPermi('system:sdk:export')")
    @Log(title = "驱动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DvDriveSdk dvDriveSdk)
    {
        List<DvDriveSdk> list = dvDriveSdkService.selectDvDriveSdkList(dvDriveSdk);
        ExcelUtil<DvDriveSdk> util = new ExcelUtil<DvDriveSdk>(DvDriveSdk.class);
        util.exportExcel(response, list, "驱动管理数据");
    }

    /**
     * 获取驱动管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:sdk:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dvDriveSdkService.selectDvDriveSdkById(id));
    }

    /**
     * 新增驱动管理
     */
    @ApiOperation("新增")
//    @PreAuthorize("@ss.hasPermi('system:sdk:add')")
    @Log(title = "驱动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DvDriveSdk dvDriveSdk)
    {
        return toAjax(dvDriveSdkService.insertDvDriveSdk(dvDriveSdk));
    }

    /**
     * 修改驱动管理
     */
//    @PreAuthorize("@ss.hasPermi('system:sdk:edit')")
    @Log(title = "驱动管理", businessType = BusinessType.UPDATE)
    @ApiOperation("修改")

    @PutMapping
    public AjaxResult edit(@RequestBody DvDriveSdk dvDriveSdk)
    {
        return toAjax(dvDriveSdkService.updateDvDriveSdk(dvDriveSdk));
    }

    /**
     * 删除驱动管理
     */
    @ApiOperation("删除")
//    @PreAuthorize("@ss.hasPermi('system:sdk:remove')")
    @Log(title = "驱动管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dvDriveSdkService.deleteDvDriveSdkByIds(ids));
    }


@ApiOperation("文件上传")
@PostMapping("/upload")
    public @ResponseBody R upload(@RequestParam MultipartFile file, DvDriveSdk dvDriveSdk){
        String upload = minIoService.upload(file);
        String adapterName = dvDriveSdk.getAdapterName();
    DvDrive dvDrive = dvDriveService.selectDvDriveByAdapterName(adapterName);
    if(dvDrive==null){
            return R.fail("驱动表尚未有此插件库名称");
    }else {
        dvDriveSdk.setPackageUrl(upload);
        dvDriveSdk.setCreateUserId(getUserId().toString());
        dvDriveSdk.setDeptId(getDeptId().toString());
        dvDriveSdkService.insertDvDriveSdk(dvDriveSdk);
    }
    return R.ok("上传成功");
    }
}
